package com.ostrich.module_home.discovery.adapter.provider

import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.provider.BaseItemProvider
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.ostrich.library_common.utils.DensityUtils
import com.ostrich.library_common.views.CustomItemDecoration
import com.ostrich.module_home.R
import com.ostrich.module_home.databinding.HomeItemDiscoverySubjectCardViewBinding
import com.ostrich.module_home.discovery.adapter.SubjectIemAdapter
import com.ostrich.module_home.discovery.entity.DiscoveryEntity

/**
 * @Author : yaotianxue
 * @Time : On 2023/10/30 15:19
 * @Description : SubjectCardProvider 专题策划：recyclerview嵌套
 */
class SubjectCardProvider(override val itemViewType: Int, override val layoutId: Int) :BaseItemProvider<DiscoveryEntity>() {
    override fun onViewHolderCreated(viewHolder: BaseViewHolder, viewType: Int) {
        DataBindingUtil.bind<HomeItemDiscoverySubjectCardViewBinding>(viewHolder.itemView)
    }

    override fun convert(helper: BaseViewHolder, item: DiscoveryEntity) {
        helper.getBinding<HomeItemDiscoverySubjectCardViewBinding>()?.viewModel = item
        helper.getBinding<HomeItemDiscoverySubjectCardViewBinding>()?.executePendingBindings()

        //子recyclerView
        val rv = helper.getView<RecyclerView>(R.id.rv_category_view)
        val subjectIemAdapter = SubjectIemAdapter(R.layout.home_item_discovery_category_item_subject_card_view,item.data.itemList.toMutableList())
        rv.adapter = subjectIemAdapter
        rv.layoutManager = GridLayoutManager(context,2)
        rv.setHasFixedSize(true)

        //添加自定义分割线
        rv.addItemDecoration(CustomItemDecoration(2,DensityUtils.dp2px(context,10f)))

    }
}